Jimg: Java Advanced Imaging Interface for ColdFusion
Jimg makes it easy to manipulate multiple types of image formats in a variety of ways, whether you scale down digital photos into thumbnails or add captions to images. Not only are these tasks simple to accomplish, but the interface allows you to combine several image operations seamlessly—for example, scaling an image, adding a border and text, and rotating the image.
Jimg is built on top of the Java Advance Imaging API (JAI). This sophisticated API makes it possible to manipulate and convert images of a variety of formats. Provided in this package are a set of custom tags, a ColdFusion component, and Java objects that allow you to choose the interface that best meets your needs.
Installing Jimg
The Jimg package consists of four installable items. They include JAR files, a set of custom tags, a ColdFusion component, and examples. In the instructions below, wwwroot refers to the web root of your ColdFusion server. Although the web root of your web server may differ from this, installing Jimg only depends on the web root of your ColdFusion server. My ColdFusion web root was found at C:\CFusionMX\wwwroot. If you use the paths I suggest, all of the examples will function properly. However, if you are an advanced user, feel free to install wherever you feel is appropriate.
Three JAR files are included in this package. The ones called jai_core.jar and jai_codec.jar
contain the class files for the JAI package. The one called jimg.jar is a custom API written on
top of the JAI to provide a more convenient front end with which the ColdFusion code can
integrate. Copy all three JAR files to wwwroot/WEB-INF/lib.
You can find the custom tags for Jimg in the folder named tags. I recommend that you copy
these tags into their own custom tag folder. Install the tags in wwwroot/cf_tags/jimg.
Copy the examples folder to a folder under wwwroot. One possible location is
wwwroot/jimg-examples.
The custom tag and CFC interfaces are mutually independent. You can install either or both.
Note that Jimg requires either Java 1.4 or "Red Sky".
You can locate the source code to the class files found in jimg.jar in the
com/macromedia/image folder. To compile the source code in Windows, navigate to
this directory and run this command (all on one line):
javac –classpath '../../..;../../../jai_codec.jar;../../../jai_core.jar' ImageManipulator.java ImageHandle.java ImageException.java
For other operating systems, the command above may vary.
Using Jimg
There are three primary ways of making use of the Jimg package: using a custom tag interface, using a ColdFusion Component interface, and writing directly to the provided Java code. Using the CFC interface and writing directly to the Java API are beyond the scope of this document. To learn more about both topics, please see the source code in jimg.cfc, examples/cfcPrompt.cfm, and cfcResults.cfm.
I can best explain the custom tag interface by way of an example. The following code snippet takes in a URL to an image, scales that image, adds a pink border, and sends the image to the browser:
1 <cfsetting enableCfOutputOnly = "yes"/> 2 <cfcontent type="image/png; charset=8859_1"> 3 <cfimport taglib="/cf_tags/jimg" prefix="img" /> 4 <cfparam name = "url.imageUrl"/> 5 <img:sequence> 6 <img:load url="#url.imageUrl#"/> 7 <img:scaleToAtLeast width="100" height="100"/> 8 <img:border width="4" color="0xFF00CC"/> 9 <img:save variableName="img"/> 10 <cfoutput>#toString(img)#</cfoutput> 11 </img:sequence>
Line 1 instructs ColdFusion not to output any white space or other characters that are not explicitly asked to be output. This is important because the purpose of this code is to produce a binary image. Any white space included by ColdFusion would corrupt the image. Line 2 sets the content type of this page to that of a PNG file. This ensures that the browser knows to render the result of this code as an image and not as HTML or text. Lines 1 and 2 exist because we are generating an image directly. If we were generating an image and saving it to a file, we wouldn't have to include these lines.
Line 3 sets up the custom tags. The path specified for the attribute taglib should be the location in which you installed the custom tags. By installing these custom tags in a unique directory and importing them with a prefix that differs from other custom tag libraries, you can avoid potential name conflicts. For example, there are custom tags named "load" and "save." It would be impractical to drop these tags into a global custom tag library because they would effectively use up two generic names. I chose the prefix "img" but it would be appropriate to choose either a shorter or longer name.
Line 4 sets up the imageUrl parameter.
Line 5 begins the sequence of image manipulations, which are contained within a set of sequence tags. (A sequence tag is responsible for gluing together the series of image operation tags contained within it.)
Lines 6–9 perform the actual image operations, which include loading an image from a URL, scaling it, adding a border, and saving the image to a ColdFusion variable.
Line 10 prints the image that results from these manipulations. You'll notice that we are converting the image to a string. Because of line 2, we have prepared ColdFusion for binary output. This means the act of converting the image to a string does not actually attempt to convert it to ASCII text.
If the above example is in a page named thumb.cfm, you can call it as follows:
<cfset thumbUrl = "thumb.cfm?imageUrl=#urlEncodedFormat(url.imageUrl)#"/>
<cfoutput>
<img src="#thumbUrl#">
</cfoutput>
It produces the following image:
Table 1 documents all of the operations that can occur between a set of sequence tags.
Table 1. Image Operation Tags
| Tag | Description | Attributes | Default | Notes |
|---|---|---|---|---|
| border | Adds a border to an image | width color |
0 0x000000 |
Color is hex string |
| crop | Cuts out a section of an image | x y width height |
0 0 100 100 |
|
| draw | Draws lines or rectangles on an image | type lineWidth color startX startY endX endY |
rectangle 10 0x000000 0 0 100 100 |
Color is hex string; type should be "line" or "rectangle" |
| fill | Fills in a rectangle on an image with a solid color | color startX startY endX endY |
0xFF0000 0 0 100 100 |
|
| height | Extracts the height of an image | returnName | height | Name of variable to set height of image |
| load | Loads an image from a file, a URL, or a variable containing binary data | filename url data |
none none none |
Only one of these three should be provided at a time; data is a variable that contains binary data of an image |
| pattern | Repeats an image to create a new image of a particular size | width height |
200 200 |
|
| rotate | Rotates an image | angle | 90 | Number of degrees |
| save | Saves an image to a file or variable | fileName variableName type |
none none png |
Either fileName or variableName should be provided, but not both; variableName is the name of a variable to which you store the binary contents of the image; type is either the string "png," "jpeg," "bmp," "pnm," or "tiff" |
| scale | Scales the image | xscale yscale |
100 100 |
Percentage values, where 100 means no scaling occurs |
| scaleToAtLeast | Scales image down to particular width and height if larger than specified | width height |
200 200 |
|
| size | Saves the size of the image to a variable | variableName type |
size png |
variableName is the name of the variable in which you store the file size (in bytes); type is the type of image from which the size should be derived; type is either the string "png," "jpeg," "bmp," "pnm," or "tiff" |
| text | Draws text on an image | text x y color fontName fontStyle fontSize |
none 0 0 0x000000 Arial none 12 |
Color is hex string; fontStyle is either normal, bold, or italics |
| tint | Changes the color of an image | red green blue |
0 0 0 |
Values between -255 and 255 state how much color to add or subtract |
| width | Extracts the width of an image | returnName | width | Name of variable to set height of image |
Implementing Jimg
At the foundation of the Jimg package is the JAI. In order to help manage the complexity of
the JAI package, a series of Java components have been built on top of it to provide a simpler
interface. You can find this interface in the Java files located in the folder
com/macromedia/image/—specifically the files ImageManipulator.java and
ImageHandle.java. The ImageHandle object provides a container for images and the
ImageManipulator object provides a method for altering ImageHandles.
The custom tag interface is built on top of this simplified JAI interface. The main pattern used by the custom tag interface is that of a sequence tag wrapping up a variety of image operation tags (scale, rotate, etc.). It is the responsibility of these tags to interconnect so that a developer using the interface needn't be bothered with the mechanics of linking multiple operations. The developer simply states which operations should be performed and the API chains together the desired actions. This ability to link the output of one tag automatically with the input of another is accomplished by accessing an ImageManinpulator and ImageHandle stored on the sequence custom tag. All of the image operations are nested under this single tag. Here they store and retrieve data. Therefore, the sequence tag creates these values and each image operation tag uses the created ImageManipulator to update the value of the current ImageHandle.
Because each image operation tag needs a sequence tag to perform setup work, it is invalid to use them outside of a sequence tag. To ensure that this does not happen, each image operation tag checks for the presence of the sequence tag in its list of ancestors.
The ColdFusion Component works in a very similar fashion to the custom tags. Instead of storing an ImageManipulator and ImageHandle in the request scope, they are stored in variables defined under the CFC tag. Each image operation is implemented as a ColdFusion function and can access these variables.
Luckily, both the custom tag interface and the CFC interface hide the fact that there are complex Java objects in use, freeing developers to concentrate on more important things.
About the Author
Ben Simon is Director of Systems Engineering at Amazing Media, Inc. He has vast experience programming in Java, perl, scheme, awk, and even a little ColdFusion. When he's not programming, Ben peruses comp.lang.functional or Linux Journal.